package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import Conexao.ConnectionManager;
import Modelo.AlunoSI;
import Modelo.Funcionario;
import Modelo.Visitante;

public class VisitanteDaoImp implements VisitanteDao{
	
	private final String INSERT = "insert into visitante (nome, motivo, documento) values (?,?,?)";
	private final String UPDATE = "update visitante set nome = ?, motivo = ?, documento = ? where acesso= ?";
	private final String DELETE = "delete from visitante where acesso = ?";
	private final String ALL = "select * from visitante";
	 private final String ID = "select * from visitante where acesso = ?";

	private static Connection connection;
	
	public VisitanteDaoImp() {
		openConnection();
	

}

	@Override
	public Visitante salvar(Visitante e) {
		try {
			PreparedStatement ps = null;

			ps = ConnectionManager.getInstance().getConnection()
					.prepareStatement(INSERT);

			ps.setObject(1, e.getNome());
			ps.setObject(2, e.getMotivo());
			ps.setObject(3, e.getDocumento());
			
			
			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(VisitanteDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		return e;
	}

	@Override
	public void alterar(Visitante e) {
		PreparedStatement ps = null;
		try {
			ps = connection.prepareStatement(UPDATE);

			
			ps.setObject(1, e.getNome());
			ps.setObject(2, e.getMotivo());
			ps.setObject(3, e.getDocumento());
			ps.setObject(4, e.getAcesso());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(VisitanteDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}	
	}

	@Override
	public void deletar(Visitante e) {
		try {
			PreparedStatement ps = null;
			ps = connection.prepareStatement(DELETE);

			ps.setObject(1, e.getAcesso());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(VisitanteDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
	}

	@Override
	public List<Visitante> findAll() {
		List<Visitante> visitantes = new ArrayList<Visitante>();

		try {
			PreparedStatement ps = null;
			ResultSet rs = null;

			ps = connection.prepareStatement(ALL);

			rs = ps.executeQuery();

			while (rs.next()) {
				Visitante visitante = new Visitante();
				visitante.setAcesso(rs.getInt("Acesso"));
				visitante.setNome(rs.getString("nome"));
				visitante.setMotivo(rs.getString("motivo"));
				visitante.setDocumento(rs.getString("documento"));
				visitantes.add(visitante);
			}

		} catch (SQLException ex) {
			Logger.getLogger(VisitanteDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		return visitantes;
	}
	

	@Override
	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	
	}

	@Override
	public void openConnection() {
		if (connection == null) {
			connection = ConnectionManager.getInstance().getConnection();
		}

		if (isConnectionClose()) {
			connection = ConnectionManager.getInstance().getConnection();
		}
	}


	@Override
	public void closeConnection() {
		if (connection != null && !this.isConnectionClose()) {
			ConnectionManager.getInstance().closeConnection(connection);
		}

	}

	@Override
	public Visitante BuscarPorAcesso(int acesso) {
		List<Visitante> visitantes = new ArrayList<Visitante>();

		try {
		PreparedStatement ps = null;
		ResultSet rs = null;

		ps = connection.prepareStatement(ID);
		ps.setObject(1, acesso);

		rs = ps.executeQuery();

		while (rs.next()) {
			Visitante visitante = new Visitante();
			visitante.setAcesso(rs.getInt("Acesso"));
			visitante.setNome(rs.getString("nome"));
			visitante.setMotivo(rs.getString("motivo"));
			visitante.setDocumento(rs.getString("documento"));
			visitantes.add(visitante);
		}

		} catch (SQLException ex) {
		Logger.getLogger(VisitanteDaoImp.class.getName()).log(Level.SEVERE,
		null, ex);
		} finally {
		this.closeConnection();
		}
		if (visitantes.isEmpty()) {
		return null;
		} else {
		return visitantes.get(0);
		}
	}
}
